﻿@inherits SsoSectionComponentBase

<MTextField Value="Value.IdCard" TValue="string" ValueChanged="ValueChangeHandler" Label="@T("IdCard")"
            Placeholder="@T("IdCardPlaceholder")" Outlined Rules="_idCardRules">
</MTextField>

@code {
    [Parameter]
    public RegisterInputModel Value { get; set; } = new();

    [Parameter]
    public EventCallback<RegisterInputModel> ValueChanged { get; set; }

    [Parameter]
    public bool Required { get; set; }

    private IList<Func<string, StringBoolean>> _idCardRules = new List<Func<string, StringBoolean>>();

    protected override Task OnParametersSetAsync()
    {
        if (!_idCardRules.Any())
        {
            _idCardRules.Add(value => Regex.Match(value, LoginOptions.IdCard).Success ? true : T("IdCardFormatError"));
        }
        if (Required && _idCardRules.Count == 1)
        {
            _idCardRules.Add(value => !string.IsNullOrEmpty(value) ? true : T("IdCardRequired"));
        }

        return base.OnParametersSetAsync();
    }

    void ValueChangeHandler(string value)
    {
        Value.IdCard = value;
    }
}